package com.steven.ourcontest.chat.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseViewBinder
import com.steven.ourcontest.chat.bean.ApplyFriend
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants

class ApplyViewBinder(
    private val context: Context?,
    private val agreeListener: ((uid: Long, agree: Boolean) -> Unit)
) : BaseViewBinder<ApplyFriend, ApplyViewHolder>() {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ApplyViewHolder {
        val mInflater = inflater.inflate(R.layout.item_apply_list, parent, false)
        return ApplyViewHolder(mInflater)
    }

    override fun onBindViewHolder(itemHolder: ApplyViewHolder, itemData: ApplyFriend) {
        itemHolder.apply {
            tv_name?.text = itemData.uname
            tv_agree?.setOnClickListener {
                agreeListener.invoke(itemData.applicantUser, true)
            }
            tv_refuse?.setOnClickListener {
                agreeListener.invoke(itemData.applicantUser, false)
            }
            if (context != null) {
                if (iv_icon != null) {
                    Glide.with(context)
                        .load(NetConstants.ICON_BASE_URL + itemData.iconUrl)
                        .error(R.drawable.ic_avatar)
                        .apply(
                            RequestOptions.bitmapTransform(GlideRoundTransform(context, 10))
                        ).into(iv_icon)
                }
            }
        }
    }
}

class ApplyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val iv_icon: ImageView? = itemView.findViewById(R.id.iv_icon)
    val tv_name: TextView? = itemView.findViewById(R.id.tv_name)
    val tv_agree: TextView? = itemView.findViewById(R.id.tv_agree)
    val tv_refuse: TextView? = itemView.findViewById(R.id.tv_refuse)
}

